<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Copy a file internal to the phar archive to another new file within the phar</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="phar.converttoexecutable.html">Phar::convertToExecutable</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="phar.count.html">Phar::count</a></div>
 <div class="up"><a href="class.phar.html">Phar</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="phar.copy" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">Phar::copy</h1>
  <p class="verinfo">(PHP &gt;= 5.3.0, PECL phar &gt;= 2.0.0)</p><p class="refpurpose"><span class="refname">Phar::copy</span> &mdash; <span class="dc-title">Copy a file internal to the phar archive to another new file within the phar</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-phar.copy-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">bool</span> <span class="methodname"><strong>Phar::copy</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$oldfile</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$newfile</code></span>
   )</div>

  <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para">此方法需要
将 <var class="filename">php.ini</var> 中的 <em>phar.readonly</em> 设为
<em>0</em> 以适合 <a href="class.phar.html" class="classname">Phar</a>
对象.  否则, 将抛出<a href="class.pharexception.html" class="classname">PharException</a>.</p></p></blockquote>

  <p class="para">
   Copy a file internal to the phar archive to another new file within the phar.
   This is an object-oriented alternative to using <span class="function"><a href="function.copy.html" class="function">copy()</a></span> with
   the phar stream wrapper.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-phar.copy-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">oldfile</code></em></dt>

     <dd>

      <p class="para">
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">newfile</code></em></dt>

     <dd>

      <p class="para">
      </p>
     </dd>

    
   </dl>

  </p>

 </div>

 <div class="refsect1 returnvalues" id="refsect1-phar.copy-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   returns <strong><code>TRUE</code></strong> on success, but it is safer to encase method call in a
   try/catch block and assume success if no exception is thrown.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-phar.copy-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">
   throws <a href="class.unexpectedvalueexception.html" class="classname">UnexpectedValueException</a> if the source file does not
   exist, the destination file already exists, write access is disabled, opening either
   file fails, reading the source file fails, or a <a href="class.pharexception.html" class="classname">PharException</a>
   if writing the changes to the phar fails.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-phar.copy-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-646">
    <p><strong>Example #1 A <span class="function"><strong>Phar::copy()</strong></span> example</strong></p>
    <div class="example-contents"><p>
     This example shows using <span class="function"><strong>Phar::copy()</strong></span> and the
     equivalent stream wrapper performance of the same thing.  The primary
     difference between the two approaches is error handling.  All Phar methods
     throw exceptions, whereas the stream wrapper uses <span class="function"><a href="function.trigger-error.html" class="function">trigger_error()</a></span>.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">try&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$phar&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Phar</span><span style="color: #007700">(</span><span style="color: #DD0000">'myphar.phar'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$phar</span><span style="color: #007700">[</span><span style="color: #DD0000">'a'</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #DD0000">'hi'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$phar</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">copy</span><span style="color: #007700">(</span><span style="color: #DD0000">'a'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'b'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$phar</span><span style="color: #007700">[</span><span style="color: #DD0000">'b'</span><span style="color: #007700">];&nbsp;</span><span style="color: #FF8000">//&nbsp;outputs&nbsp;"hi"<br /></span><span style="color: #007700">}&nbsp;catch&nbsp;(</span><span style="color: #0000BB">Exception&nbsp;$e</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;handle&nbsp;error<br /></span><span style="color: #007700">}<br /><br /></span><span style="color: #FF8000">//&nbsp;the&nbsp;stream&nbsp;wrapper&nbsp;equivalent&nbsp;of&nbsp;the&nbsp;above&nbsp;code.<br />//&nbsp;E_WARNINGS&nbsp;are&nbsp;triggered&nbsp;on&nbsp;error&nbsp;rather&nbsp;than&nbsp;exceptions.<br /></span><span style="color: #0000BB">copy</span><span style="color: #007700">(</span><span style="color: #DD0000">'phar://myphar.phar/a'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'phar//myphar.phar/c'</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'phar://myphar.phar/c'</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;outputs&nbsp;"hi"<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="phar.converttoexecutable.html">Phar::convertToExecutable</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="phar.count.html">Phar::count</a></div>
 <div class="up"><a href="class.phar.html">Phar</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
